<template>
	<div style="line-height: 2.7;padding: 15px">
		<div class="box">
			<el-row style="text-align: center">
				<el-col><span style="font-size: 1.5em;font-weight: bold">{{ viewData.sxbt }}</span></el-col>
			</el-row>
      <el-row style="line-height: 2">
        <el-col :span="12"><p>{{viewData.xdbm||'无'}}</p></el-col>
        <el-col :span="12"><span style="float: right">{{ viewData.xdrq}}</span></el-col>
      </el-row>
		</div>
		<div class="box">
			<el-row>
				<label>事项性质：</label>
				<div style="display: inline" v-for="dic in sxxzDics" v-if="dic.dictValue==viewData.sxxz">
					{{dic.dictLabel}}
				</div>
			</el-row>
      <el-row>
        <label>事项类型：</label>
        <div style="display: inline" v-for="dic in sxlxDics" v-if="dic.dictValue==viewData.sxlx">
          {{dic.dictLabel}}
        </div>
      </el-row>
			<el-row>
				<label>紧急程度：</label>
				<div style="display: inline" v-for="dic in jjcdDics" v-if="dic.dictValue==viewData.jjcd">
					{{dic.dictLabel}}
				</div>
			</el-row>
			<el-row>
				<label>是否反馈：</label>
				<div style="display: inline" v-for="dic in sffkDics" v-if="dic.dictValue==viewData.sffk">
					{{dic.dictLabel}}
				</div>
			</el-row>
			<el-row>
				<label>截止日期：</label>
				<div style="display: inline" :class="{colorHong:viewData.cqts>0}">
					<span>{{ viewData.jzrq}}</span>
					<span style="margin-left: 10px;" v-if="viewData.cqts>0">超期{{ viewData.cqts}}天</span>
				</div>
			</el-row>
			<el-row>
				<label>下达人：</label>
				<span>{{ viewData.xdr}}</span>
			</el-row>
			<el-row>
				<label>联系电话：</label>
          <span @click="makePhoneCall(viewData.lxdh)" style="color: #003cff">{{ viewData.lxdh}}</span>
          <i style="color: #003cff;" class="el-icon-phone"></i>
			</el-row>
		</div>
		<div class="box">
			<el-row>
				<label>相关文件：</label>
        <view v-for=" img in fileListView ">
<!--          <span @click="downFile(img.wjdz)">下载</span>-->
          <uni-link icon="el-icon-document-remove" style="display: block;color: #577cfa"
                   :href="img.wjdz" :download="img.wjmc"  :text="img.wjmc">
            {{ img.wjmc}}
          </uni-link>
        </view >

			</el-row>
		</div>
    <div>
      <el-row>
        <label>下达内容</label>
        <uni-easyinput  autoHeight  type="textarea"  v-model="viewData.xdnr"/>
      </el-row>
    </div>
    <div v-if="viewData.sffk==1" class="box" style="border: none">
      <el-row>
        <label>反馈内容：</label>
        <el-upload class="upload-demo" :action="uploadParam.uploadUrl" :headers="uploadParam.headers"
                   :before-upload="rwBeforeUpload" :on-change="handleChange" :on-error="handleUploadError"
                   :on-success="handleUploadSuccess" :auto-upload="true" ref="upload" multiple
                   :file-list="uploadParam.fileList">
          <el-button v-show="fkzt!=3" slot="trigger" type="primary" icon="el-icon-upload" size="small">上传</el-button>
        </el-upload>
      </el-row>
      <div>
        <el-input type="textarea" v-model="fknr"></el-input>
      </div>
    </div>
    <el-row>
      <label>转发意见：</label>
      <el-input :disabled="isleader !=1" type="textarea" v-model="zfyj"></el-input>
    </el-row>
    <div v-if="isleader === '1'">
      <el-row style="text-align: center;margin-top: 40px;">
        <el-button element-loading-text="转发中请稍后..." v-loading.fullscreen.lock="tranLoading"
                   @click="inputDialogToggle()" type="primary" style="width: 80%;background: #4196e5;">转发</el-button>
      </el-row>
    </div>
    <el-row v-if="viewData.sffk==1" style="text-align: center;margin-top: 40px;">
      <el-button v-show="fkzt!=3" v-loading.fullscreen.lock="submitLoading" @click="submitFk()" type="primary"
                 style="width: 80%;background: #4196e5;">提交反馈</el-button>
    </el-row>

    <uni-popup ref="inputDialog" type="dialog">
      <uni-popup-dialog ref="inputClose"  mode="input" title="选择部门" value="对话框预置提示内容!"
                        placeholder="请输入内容" @confirm="tranMessageByUser">
        <uni-forms-item>
          <uni-data-checkbox v-model="tranDepts" multiple :localdata="userDepts" />
        </uni-forms-item>
      </uni-popup-dialog>
    </uni-popup>

    <uni-popup ref="message" type="message">
      <uni-popup-message :type="message.type" :message="message.msg" :duration="2000"></uni-popup-message>
    </uni-popup>
	</div>

</template>

<script>
	import {
		getFkxxInfo,
    getFkIdByRwbAndUserId,
		submitFk,
		tranMessageByUser,
		updateIsTranBySid,
    updateCysjByfkId,
    getDeptByUserId
	} from "@/api/system/yjgl";
	import {
		getUserId,
      getDeptId,
    getIsLeader,
    getUsername
	} from '@/utils/auth';
	import {
		getDicts
	} from "@/api/system/dict/data";
	import config from '@/config';
  import { saveAs } from 'file-saver';
  import {downFile} from "@/utils/common";
  export default {
		name: "ckRwxx",
		dicts: ['sffk', 'sxxz'],
		data() {
			return {
        message:{
          type:"",
          msg:""
        },
        ly:'1',
        userDepts:[],
        tranDepts:[],
				sxxzDics: [],
				sxlxDics: [],
				sffkDics: [],
				jjcdDics: [],
				submitLoading: false,
				tranLoading: false,
				formLabelWidth: '50px',
				viewData: {},
				fileListView: [],
				fkId: "",
				fkzt: "",
				rwbId: "",
        fknr:'',
				uploadParam: {
					// 上传的图片服务器地址
					uploadUrl: config.baseUrl + "/common/upload",
					// headers: {
					// 	Authorization: "Bearer " + getToken()
					// },
					fileType: [
						["doc", "xls", "ppt", "txt", "pdf"],
					],
					fileList: [],
				},
				isleader: getIsLeader(),
        loadTime:0,
        unLoadTime:0,
        zfyj:""
			}
		},
		onLoad(options) {
      debugger
			this.fkId = options.fkId;
			this.isTz = options.isTz;
			this.rwbId = options.rwbId;
			this.viewData.workid = options.workid;
			this.ly = options.ly;
      this.getTranDepts();
		},
    onShow() {
      console.log("onshow")
      this.loadTime = Date.now();
    },
    onUnload() {
      this.unLoadTime = Date.now();
      console.log("onUnload", (this.unLoadTime - this.loadTime) / 1000);
      this.updateCysjByfkId();
    },
    onHide() {
      this.unLoadTime = Date.now();
      console.log("onHide", (this.unLoadTime - this.loadTime) / 1000);
      this.updateCysjByfkId();
    },
		methods: {
      downFile(wjdz) {
        downFile(wjdz);
      },
      makePhoneCall(tel){
        uni.makePhoneCall({
          phoneNumber: tel //仅为示例
        });
      },
      messageToggle(type,msg) {
        this.message.type = type
        this.message.msg = msg
        this.$refs.message.open();
      },
      async inputDialogToggle() {
        // let result = await updateIsTranBySid({sid: this.viewData.sid ,isUpdate:false,ly:this.ly}).then(res => {
        //   console.log(res)
        //   this.tranLoading = false
        //   if(res.code == 200){
        //     if ("操作成功" === res.msg){
        //       return true;
        //     }else{
        //       this.$modal.msgError(res.msg);
        //       return false;
        //     }
        //   }
        // })
        // if (result){
          this.$refs.inputDialog.open();
        // }
      },
      updateCysjByfkId(){
        updateCysjByfkId({sid: this.fkId, cysj: Math.round((this.unLoadTime - this.loadTime) / 1000)});
      },
			// 上传失败
			handleUploadError(err) {
				console.log("上传失败...")
				this.$modal.msgError("上传文件失败，请重试");
				this.$modal.closeLoading();
			},
			rwBeforeUpload(file) {
				console.log("上传前...");
			},
      getTranDepts(){
        getDeptByUserId({userId:getUserId()}).then(res=>{
          this.userDepts = res.data;
          // open 方法传入参数 等同在 uni-popup 组件上绑定 type属性
          // this.$refs.popup.open(this.type)
        })
      },
			async tranMessageByUser() {
        if (this.tranDepts.length==0){
          this.messageToggle('warn','请选择转发部门');
          return;
        }
				this.tranLoading = true
				// let result = await updateIsTranBySid({sid: this.viewData.sid ,isUpdate:true,ly:this.ly}).then(res => {
				// 	console.log(res)
				// 	this.tranLoading = false
				// 	if(res.code == 200){
				// 		if ("操作成功" === res.msg){
				// 			this.$modal.msgSuccess(res.msg);
        //       return true;
				// 		}else{
				// 			this.$modal.msgError(res.msg);
        //       return false;
				// 		}
				// 	}
				// })
				if(true){
					tranMessageByUser({
            fkId: this.fkId,
            zfyj: this.zfyj,
						rwb: this.viewData,
						// deptId: getDeptId(),
            deptIds: this.tranDepts.map(String),
            ly:'2',
            username:getUsername()
					}).then(res => {
            this.tranLoading = false
            this.$modal.msgSuccess("转发成功");
						console.log(res)
					})
				}
			},
			submitFk() {
				this.submitLoading = true;
				// this.submitUpload(); // 提交附件
				let data = {
					"fjbs": this.uploadParam.fileList,
					"sid": this.fkId,
          "fknr":this.fknr
				}
				setTimeout(res => {
					submitFk(data).then(res => {
						if (res.code == 200) {
							this.submitLoading = false;
							uni.switchTab({
								url: '/pages/index',
							})
						} else {
							this.$modal.msgError(res.msg);
						}
					})
				}, 2000)
			},
			submitUpload() {
				this.$refs.upload.submit();
			},
			handleChange(file, fileList) {
				console.log("附件发生改变...");
				console.log("file...", file);
				console.log("fileList...", fileList);
				this.uploadParam.fileList = fileList;
				console.log("uploadParam.fileList", this.uploadParam.fileList)
			},
			// 上传成功回调
			handleUploadSuccess(res, file) {
				console.log("上传成功...");
			},
		},
		created() {
			// 字典翻译
			getDicts("sxxz").then(response => {
				this.sxxzDics = response.data;
			});
      getDicts("sxlx").then(response => {
				this.sxlxDics = response.data;
			});
			getDicts("sffk").then(response => {
				this.sffkDics = response.data;
			});
			getDicts("jjcd").then(response => {
				this.jjcdDics = response.data;
			});
      debugger
      if (this.isTz){// 量子通知跳转
      getFkIdByRwbAndUserId({rwbId:this.rwbId,userId:getUserId()}).then(res=>{
        this.fkzt = res.data.zt;
        this.fkId = res.data.sid;
        this.fknr = res.data.fknr;
        getFkxxInfo({
          ly:this.ly,
          fkId: this.fkId,
          rwbId: this.rwbId,
          userId: getUserId()
        }).then(res => {
          this.viewData = res.data;
          this.zfyj = res.data.zfyj;
          this.fileListView = res.data.fjbs;
          console.log(res);
        })
      })
      }else {
        getFkxxInfo({
          ly:this.ly,
          fkId: this.fkId,
          userId: getUserId()
        }).then(res => {
          this.viewData = res.data;
          this.zfyj = res.data.zfyj;
          // this.isleader = res.isleader;
          this.fileListView = res.data.fjbs;
          console.log(res);
        })
      }
		}
	}
</script>

<style scoped>
	.box {
		border-bottom: solid #999696 1px;
	}

	label {
		margin-right: 5px;
	}

	.colorHong {
		color: red;
	}
</style>
